<template>
  <div>
    <!--
      action 必传 需要给一个后端接口地址, elementui会帮你发送请求
      我们不需要它自动发请求, 我们将来自己发送请求, 此时给空 不让他报错
    -->
    <el-upload
      :class="{ disabled: isDisabled }"
      action=""
      list-type="picture-card"
      :file-list="fileList"
      :on-preview="handlePreview"
      :limit="limit"
      :on-remove="handleRemove"
      :on-change="handleChange"
      :before-upload="handleBeforeUpload"
      :http-request="handleUpload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <!-- <el-progress
      style="width: 300px"
      :text-inside="true"
      :stroke-width="20"
      :percentage="percent"
    /> -->
    <el-dialog
      title="图片预览"
      :visible="showDialog"
      @close="showDialog = false"
    >
      <img style="display: block; width: 100%" :src="imgUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5' // 导入腾讯云的包(sdk)

const cos = new COS({
  SecretId: 'AKIDoQmxsWu1sSlnZ5KI0SdYCZfU2LNMBpGT', // 身份识别ID
  SecretKey: 'ziC6oGTQ9fz3sRkodDWaQ1MUN0NVNzMT' // 身份秘钥
})

export default {
  name: 'ImageUpload',
  props: {
    limit: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      fileList: [],
      showDialog: false,
      imgUrl: '',
      percent: 0
    }
  },
  computed: {
    isDisabled() {
      return this.fileList.length >= this.limit
    },
    isAllUploadSuccess() {
      return this.fileList.every(item => item.status === 'success')
    }
  },
  methods: {
    handlePreview(file) {
      this.showDialog = true
      this.imgUrl = file.url
    },
    handleRemove(file, fileList) {
      //   this.fileList = [...fileList]
      this.fileList = this.fileList.filter((item) => item.uid !== file.uid)
    },
    handleUpload(obj) {
      //   console.log('可以上传了', obj.file)
      const uid = obj.file.uid
      const fileObj = this.fileList.find((item) => item.uid === uid)
      console.log(fileObj)

      fileObj.status = 'uploading'

      cos.putObject(
        {
          Bucket: 'hrsaas-1308196520', // 存储桶名字
          Region: 'ap-shanghai', // 存储桶所在地域
          Key: parseInt(Math.random() * 999999) + obj.file.name, // 上传到存储桶中的文件名
          StorageClass: 'STANDARD', // 设置对象存储类型: 标准
          Body: obj.file, // 上传的文件对象
          onProgress: (progressData) => {
            fileObj.percentage = parseInt(progressData.percent * 100)
          }
        },
        (err, data) => {
          //   console.log(err || data)
          // 上传失败
          if (err) {
            console.log(err)
            return
          }
          // 上传成功
          fileObj.url = 'http://' + data.Location
          setTimeout(() => {
            fileObj.status = 'success'
          }, 800)
        }
      )
    },
    handleChange(file, fileList) {
      this.fileList = [...fileList]
    },
    handleBeforeUpload(file) {
      const isLt2M = file.size / 1204 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片不能超过 2MB!')
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>
